package org.tlipoca.smchannel.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.genntii.smcommon.constant.GatewayConstant;
import org.genntii.smcommon.result.PageResult;
import org.genntii.smcommon.result.Result;
import org.genntii.smdomain.entity.Channel;
import org.genntii.smdomain.vo.ChannelVO;
import org.springframework.web.bind.annotation.*;
import org.tlipoca.smchannel.service.ChannelService;

import java.util.ArrayList;
import java.util.Map;

@Tag(name = "/channel")
@Slf4j
@RestController
@RequestMapping("/channel")
public class ChannelController {

    @Resource
    private ChannelService channelService;

    @GetMapping("/search")
    public Result<PageResult<ChannelVO>> searchChannel(@RequestParam("keyword") String keyword,
                                            @RequestParam("pageNum") int pageNum,
                                            @RequestParam("pageSize") int pageSize){
        return Result.success(channelService.search(keyword,pageNum,pageSize));
    }

    @GetMapping("/list")
    public Result<ArrayList<ChannelVO>> getChannelVOList(){
        return Result.success(channelService.getChannelVOList());
    }

    @Operation(summary = "频道是否存在")
    @GetMapping("/exists/{channelId}")
    public Result<Boolean> channelExists(@PathVariable Long channelId) {
        return Result.success(channelService.channelExistsService(channelId));
    }

    @Operation(summary = "根据appId获取频道")
    @GetMapping("/{appid}")
    public Result<Channel> getChannelByAppId(@PathVariable Integer appid, HttpServletRequest request) {
        Long userId = Long.valueOf(request.getHeader(GatewayConstant.USER_ID));
        return Result.success(channelService.getChannelByAppIdService(appid, userId));
    }

    @Operation(summary = "根据频道ID获取频道/client")
    @GetMapping
    public Result<ChannelVO> getChannelById(@RequestParam("channelId") Long channelId, @RequestParam Long userId) {
        return Result.success(channelService.getChannelByIdService(channelId,userId));
    }

    @Operation(summary = "根据频道ID获取频道/直接请求获取")
    @GetMapping("/dc")
    public Result<Channel> getChannelByIdDC(@RequestParam("channelId") Long channelId, HttpServletRequest request) {
        Long userId = Long.valueOf(request.getHeader(GatewayConstant.USER_ID));
        return Result.success(channelService.getChannelById(channelId,userId));
    }

    @Operation(summary = "根据频道ID列表批量获取频道")
    @GetMapping("/batch")
    public Result<Map<Long, ChannelVO>> getChannelListByIds(@RequestParam("channelIds")ArrayList<Long> channelIds, @RequestParam Long userId) {
        return Result.success(channelService.getChannelListByIdsService(channelIds, userId));
    }

    @GetMapping("/channelBatch")
    public Result<ArrayList<Channel>> getChannelBatch(@RequestParam("channelIds")ArrayList<Long> channelIds){
        return Result.success(channelService.getChannelBatch(channelIds));
    }

}
